package data.output;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import data.set.OfflineSet;
import data.set.OfflineSetIterator;
import data.set.implementation.OfflineSet_Memory;
import data.vectors.DoubleVector;
import data.vectors.DoubleVectorIterator;

public class OctaveSaver {
	private String filename;

	public OctaveSaver(String filename) {
		super();
		this.filename = filename;
	}

	public void save(double[] vec){
		//String filename = filename;
		FileOutputStream output;
		try {
			output = new FileOutputStream(filename);

			PrintStream p = new PrintStream(output);

			p.println("# Created by JAVA");
			p.println("# name: data");
			p.println("# type: matrix");
			p.println("# rows: 1" );
			p.println("# columns: "+ vec.length);

			for(int i=0; i<vec.length; i++)
				p.println(vec[i]+" ");
			
			output.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	public void save(int[] vec){
		//String filename = filename;
		FileOutputStream output;
		try {
			output = new FileOutputStream(filename);

			PrintStream p = new PrintStream(output);

			p.println("# Created by JAVA");
			p.println("# name: data");
			p.println("# type: matrix");
			p.println("# rows: 1" );
			p.println("# columns: "+ vec.length);

			for(int i=0; i<vec.length; i++)
				p.println(vec[i]+" ");
			
			output.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}

	public void save(int[][] allresultsSVM) {
		FileOutputStream output;
		try {
			output = new FileOutputStream(filename);

			PrintStream p = new PrintStream(output);

			p.println("# Created by JAVA");
			p.println("# name: data");
			p.println("# type: matrix");
			p.println("# rows: "+ allresultsSVM.length );
			p.println("# columns: "+ allresultsSVM[0].length);

			for(int i=0; i<allresultsSVM.length; i++){
				for(int j=0; j<allresultsSVM[0].length; j++)
					p.print(allresultsSVM[i][j]+" ");
				p.println();
			}
			
			output.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		
	}
	
	public void save(Double[][] allresultsSVM) {
		//String filename = filename;
		FileOutputStream output;
		try {
			output = new FileOutputStream(filename);

			PrintStream p = new PrintStream(output);

			p.println("# Created by JAVA");
			p.println("# name: data");
			p.println("# type: matrix");
			p.println("# rows: "+ allresultsSVM.length );
			p.println("# columns: "+ allresultsSVM[0].length);

			for(int i=0; i<allresultsSVM.length; i++){
				for(int j=0; j<allresultsSVM[0].length; j++)
					p.print(allresultsSVM[i][j]+" ");
				p.println();
			}
			
			output.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}
	
	public void save(Double[][][] allresultsSVM) {
		//String filename = filename;
		FileOutputStream output;
		try {
			output = new FileOutputStream(filename);

			PrintStream p = new PrintStream(output);

			p.println("# Created by JAVA");
			p.println("# name: data");
			p.println("# type: matrix");
			p.println("# ndims: "+ 3 );
			p.println( allresultsSVM.length + " "+allresultsSVM[0].length+" "+allresultsSVM[0][0].length);

			for(int k=0; k<allresultsSVM[0][0].length; k++)
				for(int j=0; j<allresultsSVM[0].length; j++)
					for(int i=0; i<allresultsSVM.length; i++){
				
					
					p.println(allresultsSVM[i][j][k]);
				
			}
			
			output.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}
	
	public void save(DoubleVector vec){
		//String filename = filename;
		FileOutputStream output;
		try {
			output = new FileOutputStream(filename);

			PrintStream p = new PrintStream(output);

			p.println("# Created by JAVA");
			p.println("# name: data");
			p.println("# type: matrix");
			p.println("# rows: " +vec.getNumberNonNullValues());
			p.println("# columns: "+ 2);

			DoubleVectorIterator iter = vec.getNonNullIterator();
			while(iter.hasNext()){
				iter.next();
				p.println(iter.getCurrentFeature()+" "+iter.getCurrentValue());
			}
			
			output.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	public void saveOSI(OfflineSet<Integer> vec){
		//String filename = filename;
		FileOutputStream output;
		try {
			output = new FileOutputStream(filename);

			PrintStream p = new PrintStream(output);

			p.println("# Created by JAVA");
			p.println("# name: data");
			p.println("# type: matrix");
			p.println("# rows: " +vec.size());
			p.println("# columns: "+ 2);

			OfflineSetIterator<Integer> iter = vec.inputsetiterator();
			while(iter.hasNext()){
				iter.next();
				p.println(iter.getCurrentId()+" "+iter.getCurrentObject());
			}
			
			output.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	
	public void saveOSD(OfflineSet<Double> vec){
		//String filename = filename;
		FileOutputStream output;
		try {
			output = new FileOutputStream(filename);

			PrintStream p = new PrintStream(output);

			p.println("# Created by JAVA");
			p.println("# name: data");
			p.println("# type: matrix");
			p.println("# rows: " +vec.size());
			p.println("# columns: "+ 2);

			OfflineSetIterator<Double> iter = vec.inputsetiterator();
			while(iter.hasNext()){
				iter.next();
				p.println(iter.getCurrentId()+" "+iter.getCurrentObject());
			}
			
			output.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	
	public void saveCell(OfflineSet<Integer[]> vec){
		//String filename = filename;
		FileOutputStream output;
		try {
			output = new FileOutputStream(filename);

			PrintStream p = new PrintStream(output);

			p.println("# Created by JAVA");
			p.println("# name: data");
			p.println("# type: cell");
			p.println("# rows: " +vec.size());
			p.println("# columns: "+ 1);

			OfflineSetIterator<Integer[]> iter = vec.inputsetiterator();
			while(iter.hasNext()){
				iter.next();
				
				Integer[] index = iter.getCurrentObject();
				
				p.println("# name: <cell-element>");
				p.println("# type: matrix");
				p.println("# rows: " +1);
				p.println("# columns: "+ index.length);
				
				for(int i=0; i<index.length; i++)
					p.print(index[i]+" ");
				
				p.println();
			}
			
			output.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	
	public void saveIndex(OfflineSet<?> vec){
		//String filename = filename;
		FileOutputStream output;
		try {
			output = new FileOutputStream(filename);

			PrintStream p = new PrintStream(output);

			p.println("# Created by JAVA");
			p.println("# name: data");
			p.println("# type: matrix");
			p.println("# rows: " +vec.size());
			p.println("# columns: "+ 1);

			OfflineSetIterator<?> iter = vec.inputsetiterator();
			while(iter.hasNext()){
				iter.next();
				
				
				p.println(iter.getCurrentId());
			}
			
			output.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	
	public void saveOfflineSetDoubleVector(OfflineSet<DoubleVector> bd, int vocSize){	
		try{
			//String filename = path+"/document.txt";
			FileOutputStream output = new FileOutputStream(filename);
			PrintStream p = new PrintStream(output);

			p.println("# Created by JAVA");
			p.println("# name: data");
			p.println("# type: sparse matrix");
			p.println("# rows: " + vocSize);
			p.println("# columns: "+ bd.size());
	        
	        OfflineSetIterator<DoubleVector> iterS = bd.inputsetiterator();
	        int cpt=0;
	        while(iterS.hasNext()){
//	        	System.out.println("add an id2sen element");
	        	iterS.next();
	        	int id = iterS.getCurrentId();
	        	DoubleVector sentence = iterS.getCurrentObject();
	        	cpt++;
//	        	if(cpt%100==0)
//	        		System.out.print(cpt+" ");
//	        	System.out.println(sentence);
	        	DoubleVectorIterator vecIter = sentence.getNonNullIterator();
	        	while (vecIter.hasNext()){
	        		vecIter.next();
	        		int idFeature = vecIter.getCurrentFeature();
	        		double countFeature = vecIter.getCurrentValue();
	        		p.println(idFeature+" "+id+" "+countFeature);
	        	}
	        	
	        }
//	        System.out.println();
	        
	        output.close();
	        
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	public void saveOSDA(OfflineSet<double[]> results) {
		FileOutputStream output;
		try {
			output = new FileOutputStream(filename);

			PrintStream p = new PrintStream(output);

			// taille des matrices
			OfflineSetIterator<double[]> iter = results.inputsetiterator();
			iter.next();
			int ncolumns = iter.getCurrentObject().length;
			
			p.println("# Created by JAVA");
			p.println("# name: data");
			p.println("# type: matrix");
			p.println("# rows: " +results.size());
			p.println("# columns: "+ (ncolumns+1));

			iter = results.inputsetiterator();
			while(iter.hasNext()){
				iter.next();
				p.print(iter.getCurrentId()+" ");//+);
				for(double buf:iter.getCurrentObject())
					p.print(buf+" ");
				p.println();
			}
			
			output.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}

	public void saveOSDM(OfflineSet_Memory<double[][]> results) {
		FileOutputStream output;
		try {
			output = new FileOutputStream(filename);

			PrintStream p = new PrintStream(output);

			// taille des matrices
			OfflineSetIterator<double[][]> iter = results.inputsetiterator();
			iter.next();
			int ncolumns = iter.getCurrentObject()[0].length;
			int nlinesperitem = iter.getCurrentObject().length;
			
			p.println("# Created by JAVA");
			p.println("# name: data");
			p.println("# type: matrix");
			p.println("# rows: " +results.size()*nlinesperitem);
			p.println("# columns: "+ (ncolumns+1));

			iter = results.inputsetiterator();
			while(iter.hasNext()){
				iter.next();
				p.print(iter.getCurrentId()+" ");//+);
				for(double[] buf:iter.getCurrentObject()){
					for(double bubuf: buf) // une ligne
						p.print(bubuf+" ");
					p.println();
				}
				//p.println();
			}
			
			output.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}

	public void save(Double[] vec) {
		//String filename = filename;
		FileOutputStream output;
		try {
			output = new FileOutputStream(filename);

			PrintStream p = new PrintStream(output);

			p.println("# Created by JAVA");
			p.println("# name: data");
			p.println("# type: matrix");
			p.println("# rows: 1" );
			p.println("# columns: "+ vec.length);

			for(int i=0; i<vec.length; i++)
				p.println(vec[i]+" ");
			
			output.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}

	
}
